<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <header style="height: 100%">
                    <div align="left">
	                    <table style="width: 100%">
	                   		<tr>
	                   			<td>
		                   			<form class="form-inline" onsubmit="return false">
		                   				<div class="form-group">
											<button id="searchBt" class="layui-btn layui-btn-sm" permission="client:query"><i class="layui-icon">&#xe615;</i>搜索</button>
										</div>
									</form>
	                   			</td>
	                   			<td align="right">
	                   				<button class="layui-btn layui-btn-sm" onclick="location.href='addClient.html'" permission="client:save">
                                      <i class="layui-icon">&#xe608;</i> 添加
                                    </button>
	                   			</td>
	                   		</tr> 
	                    </table>
					</div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
                                    <th>clientId</th>
                                    <th>授权类型</th>
                                    <th>access_token有效期（单位秒）</th>
									<th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
        </div>
    </div>
</body>


<script src="../../js/constant.js"></script>
<script type="text/javascript" src="../../js/libs/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript">
var pers = checkPermission();
var example;

layui.use(['layer'], function(){
    var layer = layui.layer;

    function init(){
    	example = 
        	$('#dt-table').DataTable({
            	"searching": false,
            	"processing": false,
            	"serverSide" : false,
            	"ordering" :false,
            	"language": {
                    "url": "../../js/plugin/datatables/Chinese.lang"
                },
            	"ajax": {
            		"url" : domainName + "/api-o/clients",
            		"type":"get",
            		"data":function(d){
            		},
            		"dataSrc": function (json) {
            			json.recordsTotal=json.total;
            			json.recordsFiltered=json.total;
            			return json.data;
            		},
        			"error":function(xhr, textStatus, errorThrown){
        				var msg = xhr.responseText;
        				console.log(msg);
        				if (xhr.status == 400) {
        					layer.msg(JSON.parse(msg).message);
        				} else if (xhr.status == 401) {
        					localStorage.removeItem("token");
        					layer.msg("token过期，请重新登录", {shift: -1, time: 1000}, function(){
                                location.href = loginPage;
                            });
        				} else if (xhr.status == 403) {
        					layer.msg('未授权');
        				} else if (xhr.status == 500) {
        					var info = JSON.parse(msg);
        					var exception = info.exception;
        					var message = info.message;
        					layer.msg('系统错误：' + (exception ? exception : message));
        				}
        			}
            	},
            	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
                "columns": [
                	{ "data": "client_id", "defaultContent": ""},
                    {
                    	"data": "authorized_grant_types",
                    	"defaultContent": "",
                    	"render": function (data, type, row) {
                    		return eval(data);
                    	}
                    },
                    { "data": "access_token_validity", "defaultContent": ""},
                    {
                    	"data": "", 
                    	"defaultContent": "",
                    	"render": function (data, type, row) {
                            var client_id = row['client_id'];
                            var href = "updateClient.html?client_id=" + client_id;
                            var edit = buttonEdit(href, "client:update", pers);
                            var del = buttonDel(client_id, "client:del", pers);

                            //重置密码
                            var resetPassword = "";
                            if($.inArray("client:update", pers) >= 0){
                                var btn = $("<button class='layui-btn layui-btn-xs'><i class='layui-icon'>重置密码</i></button>");
                                btn.attr("onclick", 'resetPassword(\"' +client_id+'\")');
                                resetPassword = btn.prop("outerHTML");
                            }

                            if(row['isSystem']){
                                del = "<span style='color:red'>(系统)</span>";
                                resetPassword = "";
                            }

                            return edit + resetPassword + del;
                      	}
                    },
                ],
            } );
    }

    init();
});

function del(id){
    layer.confirm('确定要删除吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            type : 'delete',
            url : domainName + "/api-o/clients/"+id,
            success : function(data) {
                example.ajax.reload();
                layer.msg("删除成功");
            }
        });

        layer.close(1);
    });
}

function resetPassword(id){
    layer.open({
        title:"重置密码",
        content : '<input id="psw" type="text" class="form-control" value="88888888">',
        yes : function(index, layero) {
            var secret = $('#psw').val();
            if(secret == ""){
                layer.msg("密码不能为空");
            } else {
                $.ajax({
                    type : 'put',
                    url : domainName + '/api-o/clients/'+id+'?secret='+secret,
                    success : function(data) {
                        layer.msg("成功", {shift: -1, time: 1000}, function(){
                            layer.close(index);
                        });
                    }
                });
            }
        }
    });
}

$("#searchBt").click(function(){
	example.ajax.reload();
});
</script>
</html>